package com.firmye.codereposboot.controller;

import com.firmye.codereposboot.config.security.CustomUserDetails;
import com.firmye.codereposboot.model.commons.CustResponse;
import com.firmye.codereposboot.model.user.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
public class BaseController {
    public CustResponse success(String msg, Object obj) {
        CustResponse cr = new CustResponse(100, msg);
        if(log.isDebugEnabled()) {
            log.debug("=====> Object: " + obj);
        }
        cr.setDetails(obj);
        return cr;
    }

    public CustResponse error(String msg, Exception e) {
        CustResponse cr = new CustResponse(101, msg);
        log.error("=====> Exception:", e);
        return cr;
    }

    public User getUser() {
        CustomUserDetails customUserDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return customUserDetails.getUser();
    }
}
